package com.ego.sender.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author ghp
 */

@Configuration
public class SenderConfig {

    @Value("content")
    private String contentQueue;
    @Value("${ego.rabbitmq.item.insertName}")
    private String itemInsert;
    @Value("${ego.rabbitmq.item.deleteName}")
    private String itemDelete;
    /**
     *如果没有队列帮助创建队列
     * @return
     */
    @Bean
    public Queue queue(){
        return new Queue(contentQueue);
    }


    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange("amq.direct");
    }

    /**
     * 参数名和方法名一直就是从spring容器中获取对应方法的返回值
     * @param queue
     * @param directExchange
     * @return
     */
    @Bean
    public Binding binding(Queue queue, DirectExchange directExchange){
        return BindingBuilder.bind(queue).to(directExchange).withQueueName();
    }

    /*
   写下面两个方法的目的：
   第一次发送消息时，如果没有队列创建队列，并绑定到amq.direct交换器上
    */
    @Bean
    public Queue queueItemInsert() {
        return new Queue(itemInsert);
    }

    @Bean
    public Binding binding2(Queue queueItemInsert, DirectExchange directExchange) {
        return BindingBuilder.bind(queueItemInsert).to(directExchange).withQueueName();
    }


    @Bean
    public Queue queueItemDelete() {
        return new Queue(itemDelete);
    }

    @Bean
    public Binding binding3(Queue queueItemDelete, DirectExchange directExchange) {
        return BindingBuilder.bind(queueItemDelete).to(directExchange).withQueueName();
    }
}
